package cn.z2huo.leetcode.problem.NO_0003;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author z2huo
 */
@Deprecated
class Solution3 {
	public int getResult(String str) {
		String exam;
		String demo = null;
		String flag = null;
		int result = 0;
		List list = new ArrayList();
		
		//具体操作，重点
		for(int i=0; i<str.length(); i++) {
			int j;
			for(j=i+1;j<str.length(); j++) {
				exam = str.substring(j, j+1);
				demo = str.substring(i, j);
				if(demo.contains(exam)) {
					flag = str.substring(i, j);
					break;
				}
			}
			
			list.add(flag);
		}
		System.out.println(list);
		result = getLeastStr(list);
		return result;
	}
	
	public int getLeastStr(List list) {
		String first = null;
		String second = null;
		int length = 0;
		
		//比较list中的各个字符串的长度，返回最长字符串的长度
		for(int i=0; i<list.size();) {
			first = (String) list.get(i);
			int j;
			for(j=i; j<list.size(); j++) {
				second = (String)list.get(j);
				if(second.length() > first.length()) {
					length = second.length();
					i = j;
					break;
				}else {
					length = first.length();
				}
			}
			if(j==list.size()) {
				break;
			}
		}
		return length;
	}
	
	public static void main(String[] args) {
		String first = null;
		int result = 0;
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入初始字符串：");
		if(sc.hasNextLine()) {
			first = sc.nextLine();
		}
		Solution3 s = new Solution3();
		result = s.getResult(first);
		System.out.println("最长的子字符串是："+result);
		sc.close();
	}
}